home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: undergrad.math.uwaterloo.ca!clgonsal
- From: clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves)
- Subject: Functions as parameters
- Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
- Message-ID: <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
- Date: Wed, 21 Feb 1996 16:40:53 GMT
- Nntp-Posting-Host: cayley.uwaterloo.ca
- Organization: University of Waterloo
-
-
- I've often seen and even written code that uses function pointers like
- this:
-
-
- /* cut here */
- #include <stdio.h>
-
- void foo( int (*func)( int ) ){
- printf("%d\n", func( 5 ) );
- }
-
- int bar( int x ){
- return x*2;
- }
-
- int
- main(){
- foo( bar );
- return 0;
- }
- /* cut here */
-
- Recently though, I saw a similar piece of code that did something like
- this:
-
- /* cut here */
- void foo( int func( int ) ){
- printf("%d\n", func( 5 ) );
- }
- /* cut here */
-
- I tried compiling this with both Watcom C 10.0 and GNU C 2.6.3, and it
- worked. So my question is: is this standard, or is this some weird compiler
- extension. I've never seen this syntax before. Does it mean the same thing
- as the first piece of code? The body of the two functions is identical, and
- they're called in the same way.
-
- If they are identical, why is the (*func)(int) syntax so much more common?
- The func(int) syntax does seem easier to type as well as read, but I've
- never seen it before.
-
- --
- Carl Laurence Gonsalves - clgonsal@undergrad.math.uwaterloo.ca
- Computer Science, University of Waterloo
- http://www.undergrad.math.uwaterloo.ca/~clgonsal/
- http://www.csclub.uwaterloo.ca/~clgonsal/
-